//
//  PersonTagPopViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/4.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "PersonTagPopViewController.h"

#import "ACMacros.h"
#import "User.h"

#import <TSMessages/TSMessageView.h>
#import <MobClick.h>

@interface PersonTagPopViewController ()

@end

@implementation PersonTagPopViewController {
    NSMutableArray *selectedTags;
    NSMutableArray *selectedHobbies;
    
    IBOutletCollection(UIButton) NSArray *alltags;
    IBOutletCollection(UIButton) NSArray *liveTagButtons;
    IBOutletCollection(UIButton) NSArray *natureTagButtons;
    IBOutletCollection(UIButton) NSArray *hobbyTagButtons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    selectedTags = [[NSMutableArray alloc]init];
    selectedHobbies = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.navigationController.navigationBarHidden = true;
    [MobClick beginLogPageView:@"个人标签页"];
    [self configureButtons];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.navigationController.navigationBarHidden = false;
    [MobClick endLogPageView:@"个人标签页"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Configuring the view’s layout behavior
- (void)configureButtons {
    for (UIButton *button in alltags) {
        [button.layer setCornerRadius:5];
        [button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [button setTitleColor:RGBACOLOR(255, 118, 108, 1) forState:UIControlStateSelected];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTintColor:[UIColor clearColor]];
        [button setBackgroundColor:[UIColor clearColor]];
        [button.layer setBorderWidth:1];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - Triggering Methods 
- (void)buttonClicked:(UIButton *)sender {
    if (!sender.isSelected) {
        if ([hobbyTagButtons containsObject:sender]) {
            if (selectedHobbies.count == 3) {
                [TSMessage showNotificationInViewController:self title:@"不能选择更多了" subtitle:@"您最多可选择3个兴趣标签" type:TSMessageNotificationTypeWarning duration:2.0 canBeDismissedByUser:true];
            } else if (selectedTags.count == 6) {
                [TSMessage showNotificationInViewController:self title:@"不能选择更多了" subtitle:@"您最多可选择6个标签" type:TSMessageNotificationTypeWarning duration:2.0 canBeDismissedByUser:true];
            } else {
                [sender.layer setBorderColor:[RGBACOLOR(255, 118, 108, 1) CGColor]];
                [selectedHobbies addObject:sender.titleLabel.text];
                [selectedTags addObject:sender.titleLabel.text];
                sender.selected = !sender.isSelected;
            }
        } else {
            if (selectedTags.count == 6) {
                [TSMessage showNotificationInViewController:self title:@"不能选择更多了" subtitle:@"您最多可选择6个标签" type:TSMessageNotificationTypeWarning duration:2.0 canBeDismissedByUser:true];
            } else {
                [sender.layer setBorderColor:[RGBACOLOR(255, 118, 108, 1) CGColor]];
                [selectedTags addObject:sender.titleLabel.text];
                sender.selected = !sender.isSelected;
            }
        }
    } else {
        [sender.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        if ([hobbyTagButtons containsObject:sender]) {
            [selectedHobbies removeObject:sender.titleLabel.text];
        }
        [selectedTags removeObject:sender.titleLabel.text];
        sender.selected = !sender.isSelected;
    }
}

- (IBAction)clickToCancelPop:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}

- (IBAction)clickToDone:(UIButton *)sender {
    User *user = [User sharedUser];
    user.tags = selectedTags;
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}

@end
